//
//  ProductInformationShowViewController.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/30.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "ProductInformationShowViewController.h"
#import "XXTool.h"
#import "ProductInformationShowCell.h"
#import "XVCProductDetailShow.h"
#import "XMLProductInformationShowModelArray.h"

#import "XVVConditionalSearch.h"
#import "XVCIndexSelect.h"

@interface ProductInformationShowViewController ()<UITableViewDelegate,UITableViewDataSource,XVVConditionalSearchDelegate>

@property (nonatomic,strong) XVVConditionalSearch *searchView;

@property (nonatomic,strong) UITableView *tableview;

@end

@implementation ProductInformationShowViewController
{
    XMLProductInformationShowModelArray * useData;
    NSArray <XMLSearchConditionShowModel *> *searchConditionArray;
    ProductInformationShowType showType;
    NSString *searchUseText;
}

-(XVVConditionalSearch*)searchView
{
    if (_searchView==nil)
    {
        _searchView = [[XVVConditionalSearch alloc] init];
        [_searchView setUseData:searchConditionArray];
        _searchView.delegate = self;
        [self.view addSubview:_searchView];
    }
    return _searchView;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReFeren)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(void)initParameter
{
    searchConditionArray = [XMLSearchConditionShowModel localSearchConditionData];
    showType = ProductInformationShowWithAll;
}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"展品信息", nil)];
    [self searchView];
    [self tableview];
}

-(void)initData
{
    [self.view startLoading];
    [self getData];
}

-(void)refreshLayout:(CGRect)frame
{
    _searchView.frame = CGRectMake(0, 0, frame.size.width, [_searchView getViewHeight]);
    _tableview.frame = CGRectMake(0, 45, frame.size.width
                                  , frame.size.height-45);
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* reuseIdentifier = @"ProductInformationShowCell";
    ProductInformationShowCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell==nil)
    {
        cell = [[ProductInformationShowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    [cell setCellWithData:useData.list[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return useData.list.count;
}

#pragma mark tableviewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    XMLProductInformationShowModel *temp = useData.list[indexPath.row];
    NSString *exName = @"";
    if ([XXTool didCurrentLanguageChinese])
    {
        exName = temp.cpname;
    }
    else
    {
        exName = temp.cpname_en;
    }
    [XVCProductDetailShow pushProductDetailVCWithSourceVC:self goods_id:temp.goods_id exhibitorName:exName];
}

#pragma mark XVVConditionalSearchDelegate
-(void)XVVConditionalSearchDidClickTableViewWithCellIndexPath:(NSIndexPath *)indexPath
{
    XMLSearchConditionShowModel * temp = searchConditionArray[indexPath.row];
    
    if ([temp.leftString isEqualToString:NSLocalizedString(@"国家", nil)])
    {
        [XVCIndexSelect pushIndexSelectVCWithSourceVC:self isUseCountryShow:YES nowType:XVCIndexSelectUseSearchProduct];
    }
    else if ([temp.leftString isEqualToString:NSLocalizedString(@"类别", nil)])
    {
        [XVCIndexSelect pushIndexSelectVCWithSourceVC:self isUseCountryShow:NO nowType:XVCIndexSelectUseSearchProduct];
    }

}

-(void)conditionalSearch:(XVVConditionalSearch *)seachView serachBarDidClickSearchButtonWithSearchText:(NSString *)searchText
{
    searchUseText = searchText;
    showType = ProductInformationShowWithNameSearch;
    [self getData];
}

-(void)getData
{
    if (showType==ProductInformationShowWithAll)
    {
        [XMLProductInformationShowModelArray sendRequestToGetProductInformationShowListWithListCount:0 completion:^(BOOL isSuccess, XMLProductInformationShowModelArray *model) {
            if (isSuccess)
            {
                useData = model;
                [_tableview reloadData];
            }
            [_tableview.mj_header endRefreshing];
            [self.view stopLoading];
        }];
    }
    else
    {
        [XMLProductInformationShowModelArray
         requestProuductSearchResultsListWithSearchKey:searchUseText
         searchType:@"1"
         Count:0
         completion:^(BOOL isSuccess, NSMutableArray<XMLProductInformationShowModel *> *datalist) {
             if (isSuccess)
             {
                 if (datalist.count==0)
                 {
                     [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无相关搜索", nil)];
                 }
                 
                 useData.list = datalist;
                 [_tableview reloadData];
             }
             [_tableview.mj_header endRefreshing];
         }];
    }
}

-(void)footerReFeren
{
    if (showType==ProductInformationShowWithAll)
    {
        [XMLProductInformationShowModelArray sendRequestToGetProductInformationShowListWithListCount:useData.list.count completion:^(BOOL isSuccess, XMLProductInformationShowModelArray *model) {
            if (isSuccess)
            {
                if (model.list.count==0)
                {
                    [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多展品", nil)];
                }
                [useData.list addObjectsFromArray:model.list];
                [_tableview reloadData];
            }
            [_tableview.mj_footer endRefreshing];
        }];
    }
    else
    {
        [XMLProductInformationShowModelArray
         requestProuductSearchResultsListWithSearchKey:searchUseText
         searchType:@"1"
         Count:useData.list.count
         completion:^(BOOL isSuccess, NSMutableArray<XMLProductInformationShowModel *> *datalist) {
             if (isSuccess)
             {
                 if (datalist.count==0)
                 {
                     [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多展品", nil)];
                 }
                 
                 [useData.list addObjectsFromArray:datalist];
                 [_tableview reloadData];
             }
             [_tableview.mj_footer endRefreshing];
         }];
    }
}

@end
